// SPDX-FileCopyrightText: Copyright 2025-2025 深圳市同心圆网络有限公司
// SPDX-License-Identifier: Apache-2.0

package resource_dao

import (
	"context"

	"gitcode.com/lvupclub/lvup-api/dao/common"
	"gitcode.com/lvupclub/proto-gen-go.git/src/resource_exam_api"
	"go.mongodb.org/mongo-driver/v2/bson"
	"go.mongodb.org/mongo-driver/v2/mongo"
	"go.mongodb.org/mongo-driver/v2/mongo/options"
	"google.golang.org/protobuf/proto"
	"google.golang.org/protobuf/types/known/anypb"
)

type BasicQuestionInfoDbItem struct {
	QuestionData []byte `bson:"questionData"`
}

type QuestionInfoDbItem struct {
	QuestionId    string                  `bson:"_id"`
	ResourceId    string                  `bson:"resourceId"`
	QuestionIndex uint32                  `bson:"questionIndex"`
	BasicInfo     BasicQuestionInfoDbItem `bson:"basicInfo"`
	CreateTime    int64                   `bson:"createTime"`
	UpdateTime    int64                   `bson:"updateTime"`
}

func (item *QuestionInfoDbItem) ToQuestionInfo() (*resource_exam_api.QuestionInfo, error) {
	question := &anypb.Any{}
	err := proto.Unmarshal(item.BasicInfo.QuestionData, question)
	if err != nil {
		return nil, err
	}
	return &resource_exam_api.QuestionInfo{
		QuestionId: item.QuestionId,
		BasicInfo: &resource_exam_api.BasicQuestionInfo{
			Question: question,
		},
		QuestionIndex: item.QuestionIndex,
		CreateTime:    item.CreateTime,
		UpdateTime:    item.UpdateTime,
	}, nil
}

type _QuestionInfoDao struct {
	mc *mongo.Collection
}

func newQuestionInfoDao(db *mongo.Database) (*_QuestionInfoDao, error) {
	mc := db.Collection(common.DB_NS_RESOURCE + "question_info")
	_, err := mc.Indexes().CreateOne(context.Background(), mongo.IndexModel{
		Keys: bson.D{{"resourceId", 1}},
	})
	if err != nil {
		return nil, err
	}
	return &_QuestionInfoDao{mc: mc}, nil
}

func (db *_QuestionInfoDao) Insert(ctx context.Context, dbItem *QuestionInfoDbItem) error {
	_, err := db.mc.InsertOne(ctx, dbItem)
	return err
}

func (db *_QuestionInfoDao) Exist(ctx context.Context, resourceId, questionId string) (bool, error) {
	count, err := db.mc.CountDocuments(ctx, bson.M{"resourceId": resourceId, "_id": questionId})
	return count > 0, err
}

func (db *_QuestionInfoDao) Get(ctx context.Context, resourceId, questionId string) (*QuestionInfoDbItem, error) {
	retItem := &QuestionInfoDbItem{}
	err := db.mc.FindOne(ctx, bson.M{"resourceId": resourceId, "_id": questionId}).Decode(retItem)
	return retItem, err
}

func (db *_QuestionInfoDao) Remove(ctx context.Context, resourceId, questionId string) error {
	_, err := db.mc.DeleteOne(ctx, bson.M{"resourceId": resourceId, "_id": questionId})
	return err
}

func (db *_QuestionInfoDao) RemoveByResource(ctx context.Context, resourceId string) error {
	_, err := db.mc.DeleteMany(ctx, bson.M{"resourceId": resourceId})
	return err
}

func (db *_QuestionInfoDao) Count(ctx context.Context, resourceId string) (uint32, error) {
	count, err := db.mc.CountDocuments(ctx, bson.M{"resourceId": resourceId})
	return uint32(count), err
}

func (db *_QuestionInfoDao) CountById(ctx context.Context, resourceId string, questionIdList []string) (uint32, error) {
	if len(questionIdList) == 0 {
		return 0, nil
	}
	count, err := db.mc.CountDocuments(ctx, bson.M{"resourceId": resourceId, "_id": bson.M{"$in": questionIdList}})
	return uint32(count), err
}

func (db *_QuestionInfoDao) List(ctx context.Context, resourceId string) ([]*QuestionInfoDbItem, error) {
	cursor, err := db.mc.Find(ctx, bson.M{"resourceId": resourceId}, options.Find().SetSort(bson.M{"questionIndex": 1}))
	if err != nil {
		return nil, err
	}
	retList := []*QuestionInfoDbItem{}
	err = cursor.All(ctx, &retList)
	return retList, err
}

func (db *_QuestionInfoDao) UpdateBasic(ctx context.Context, resourceId, questionId string, basicItem *BasicQuestionInfoDbItem, updateTime int64) error {
	_, err := db.mc.UpdateOne(ctx, bson.M{"resourceId": resourceId, "_id": questionId}, bson.M{
		"$set": bson.M{
			"basicInfo":  basicItem,
			"updateTime": updateTime,
		},
	})
	return err
}

func (db *_QuestionInfoDao) UpdateIndex(ctx context.Context, resourceId, questionId string, questionIndex uint32, updateTime int64) error {
	_, err := db.mc.UpdateOne(ctx, bson.M{"resourceId": resourceId, "_id": questionId}, bson.M{
		"$set": bson.M{
			"questionIndex": questionIndex,
			"updateTime":    updateTime,
		},
	})
	return err
}

func (db *_QuestionInfoDao) IncIndex(ctx context.Context, resourceId string, questionIndex uint32) error {
	_, err := db.mc.UpdateMany(ctx, bson.M{"resourceId": resourceId, "questionIndex": bson.M{"$gte": questionIndex}},
		bson.M{"$inc": bson.M{"questionIndex": 1}})
	return err
}
